/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author CCD gruppe 2
 */

import java.sql.*;
import Module.*;
import java.util.ArrayList;


public class ActionListController {
    /**
     * Database object used to connect to the database
     */
    private Database database = new Database();

    /**
     * Method that is used to get the actionlist
     * @param projectId
     * @return
     */
    public ArrayList<ActionList> getActionlist(int projectId){
        Connection conn = null;
        ArrayList<ActionList> liste = new ArrayList<ActionList>();
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from activity where projectId =? and onActionList=1");
            statement.setInt(1, projectId);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                liste.add(new ActionList(rs.getInt(1),rs.getInt(2),rs.getString(3) , rs.getString(4),
                        rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)));
            }
            rs.close();
            conn.close();
            return liste;

        }catch(Exception e){
            System.err.println(e);
        }
        return liste;
    }
    /**
     * Method used to edit a actionList
     * @param liste
     */
    public void editActionList(ArrayList<ActionList> liste){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement(""
                    + "update activity set activityName=?, startDate=?, endDate=?, responsible=?, status=?, description=? where activityId=?");
            for(int i=0; i<liste.size();i++){
                statement.setString(1, liste.get(i).getName());
                statement.setString(2, liste.get(i).getStartDate());
                statement.setString(3, liste.get(i).getEndDate());
                statement.setString(4, liste.get(i).getResponsible());
                statement.setString(5, liste.get(i).getStatus());
                statement.setString(6, liste.get(i).getDescription());
                statement.setInt(7, liste.get(i).getId());
                statement.executeUpdate();
            }
            conn.close();

        }catch(Exception e){
            System.err.println(e);
        }
    }
    /**
     * Method used to set the intro to the actionList
     * @param projectId
     * @param intro
     */
    public void setIntroduktion(int projectId, String intro){
        //projectDecionActionIntroduction
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement(""
                    + "update projectDecionActionIntroduction set actionListIntro=? where projectId =? ");
            statement.setString(1,intro);
            statement.setInt(2,projectId);
            statement.executeUpdate();
            conn.close();

        }catch(Exception e){
            System.err.println(e);
        }
    }
    /**
     * Method used to get the intro to a actionList
     * @param projectId
     * @return intro
     */
    public String getActionListIntro(int projectId){
        Connection conn = null;
        String value="";
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement(""
                    + "select actionListIntro from  projectDecionActionIntroduction where projectId=?");
            statement.setInt(1,projectId);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                value=rs.getString(1);
            }
            rs.close();
            conn.close();
            return value;

        }catch(Exception e){
            System.err.println(e);
        }
        return value;
    }
    /**
     * Method used to delete an actionlist entry
     * @param decisionList
     * @return
     */
    public boolean deletActionList(ActionList actionList) {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from activity where projectId=? and onActionList=1");
            statement.setInt(1, actionList.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        } catch (Exception e) {
            System.err.println(e);
        }

        return false;
    }

}

